/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.data.repository.beetlsql.config;

import info.peigen.hotpot.component.data.repository.beetlsql.handler.EntityEnableParaExtend;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.InterceptorContext;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.solon.annotation.Db;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;

/**
 * <b>(EntityEnableConfiguration)</b>
 *
 * @author Lihai
 * @version 1.0.0
 * @since 2023/8/23
 */
@Configuration
public class EntityEnableConfiguration implements Interceptor {

    @Bean
    public void addInterceptor(@Db("dataSource") SQLManager sqlManager) {
        // 设置false，支持lombok非javabean支持，链式调用
        SQLManager.javabeanStrict(false);

        // 逻辑删除改变了deleteById的sql，但对于其他内置查询，没有把逻辑删除作为过滤条件，依然能查询出来。这点不同于mybatis-plus。如果需要过滤，请参考扩展BeetlSQL3
        sqlManager.setQueryLogicDeleteEnable(true);

        // 仅在beetlsql自动生成的insert或update生效
        EntityEnableParaExtend enableParaExtend = new EntityEnableParaExtend();
        sqlManager.getSqlManagerExtend().setParaExtend(enableParaExtend);

        // TODO 等beetlsql作者李家智给拦截器开了口子后，做多租户数据拦截 sqlManager.setInters(new Interceptor[]{this});
    }

    @Override
    public void before(InterceptorContext ctx) {

    }

    @Override
    public void after(InterceptorContext ctx) {

    }

    @Override
    public void exception(InterceptorContext ctx, Exception ex) {

    }
}
